Ext.define('DragonApp.view.property.MonsterGrid', {
    extend: 'Ext.grid.Panel',
    alias: ['widget.propertymonster'],

    requires: [
        'DragonApp.store.Monster',
        'Ext.grid.filters.Filters',
        'Ext.overrides.locale.zh_CN.Filters'
    ],

    plugins: 'gridfilters',

    height: 600,
    margin: '0 10 0 0',
    style: {
        borderColor: '#cfcfcf',
        borderStyle: 'solid',
        border: 1
    },

    title: '佣兵',

    store: {
        type: 'monster'
    },
    columns: [
        { xtype: 'rownumberer', text: '序号', width: 70, align: 'center' },
        {
            text: '佣兵ID',
            dataIndex: 'id',
            hideable: false,
            align: 'center',
            filter: {
                type: 'number',
                itemDefaults: {
                    emptyText: '请输入佣兵ID...'
                }
            }
        }, {
            text: '佣兵名称',
            dataIndex: 'name',
            flex: 1,
            align: 'center',
            hideable: false,
            filter: {
                type: 'string',
                itemDefaults: {
                    emptyText: '请输入佣兵名称...'
                }
            }
        },
        {
            text: '定位',
            dataIndex: 'location',
            hideable: false,
            align: 'center',
            filter: {
                type: 'list',
                options: ['肉盾', '输出', '突袭']
            }
        },
        {
            text: '品质',
            dataIndex: 'quality',
            width: 80,
            hideable: false,
            align: 'center',
            filter: {
                type: 'list',
                options: ['普通', '稀有', '史诗', '传说']
            }
        },
        {
            text: '怒气消耗',
            dataIndex: 'need_angry',
            width: 90,
            hideable: false,
            align: 'center',
            filter: {
                type: 'number',
                itemDefaults: {
                    emptyText: '请输入怒气消耗值...'
                }
            }
        },
        {
            xtype: 'widgetcolumn',
            text: '操作',
            width: 120,
            sortable: false,
            align: 'center',
            hideable: false,
            menuDisabled: true,
            widget: {
                xtype: 'button',
                text: '添加',
                width: 80,
                iconCls: "x-fa fa-plus-circle",
                tooltip: '选取佣兵',
                handler: 'onAdd'
            },
            itemId: 'add'
        }
    ],

    listeners: {
        addrecord: function(record) {

            var formpanel = Ext.create('Ext.form.Panel', {
                width: 320,
                margin: 15,
                centered: true,
                record: record,
                viewModel: {
                    data: {
                        property: record
                    }
                },
                defaults: {
                    xtype: 'textfield',
                    anchor: '90%',
                    labelWidth: 70,
                    allowBlank: false,
                    msgTarget: 'side',
                    labelAlign: "left",
                    enableKeyEvents: true,
                    listeners: {
                        specialKey: 'onTextFieldSpecialKey'
                    }
                },
                items: [{
                    fieldLabel: '佣兵ID',
                    editable: false,
                    name: 'id',
                    bind: '{property.id}'
                }, {
                    fieldLabel: '佣兵名称',
                    editable: false,
                    name: 'name',
                    bind: '{property.name}'
                }, {
                    fieldLabel: '佣兵数量',
                    xtype: 'numberfield',
                    editable: true,
                    minValue: 1,
                    value: 1,
                    name: 'num'
                }]
            });

            var win = Ext.create('Ext.window.Window', {
                title: '添加佣兵',
                modal: true,
                width: 350,
                height: 200,
                resizable: false, //尺寸固定
                closeAction: 'destroy', //关闭销毁
                items: [formpanel],
                buttons: [{
                    text: '取消',
                    handler: function(button) {
                        button.up('window').down('form').destroy();
                        button.up('window').destroy();
                    }
                }, {
                    text: '确定',
                    handler: function(button) {
                        var window = button.up('window'),
                            form = window.down('form'),
                            values = form.getValues();

                        var new_record = Ext.create('DragonApp.model.SelectedProp', {
                            id: record.get('id'),
                            name: record.get('name'),
                            num: values.num
                        });

                        //增加记录
                        var selectedPropStore = Ext.data.StoreManager.lookup('selected_prop');
                        selectedPropStore.add(new_record);

                        //销毁窗体
                        form.destroy();
                        window.destroy();
                    }
                }]
            }).show();
        }
    }
});